from django.http import HttpResponse, JsonResponse
from django.views.decorators.http import require_http_methods
import base64, json
from ..models import Record
from urllib.parse import urlencode
import threading
from django.views.decorators.csrf import csrf_exempt

def_response_o = {
    "code": 0,
    "result": "",
    "message": "",
    "type": 'success',
  }

user_info_def =  {
    "userId": '1',
    "username": 'admin',
    "realName": 'Gentlebands.admin',
    "avatar": '',
    "desc": 'manager',
    # "password": '123456',
    "token": 'fakeToken1',
    "roles": [
    {
        "roleName": 'Super Admin',
        "value": 'super',
    },
    ],
}

@require_http_methods(["POST"])
@csrf_exempt
def login(request):
    params = json.loads(request.body)
    if params['username']=='admin_user@tmp.usr' and params['password']=='6211388071174dc9808f776a2fc4e771':
        re = def_response_o
        re['result'] = user_info_def
        return JsonResponse(re)
    else:
        re = def_response_o
        re['message'] = "用户及密码错误，请重试!"
        re['result'] = {}
        return JsonResponse(re)

@require_http_methods(["GET"])
def get_user_info(request):
    params = request.GET
    re = def_response_o
    re['result'] = user_info_def
    return JsonResponse(re)